﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using xTeam.Hengle.DataAccess;

namespace xTeam.HengLe.Controller
{
    public class BaseController : System.Web.Mvc.Controller
    {
        protected string _connectionString;
        protected static ClassifyDataAccess _classifyDataAccess;
        protected static ProductDataAccess _productDataAccess;
        protected const string _imageRoot = "/ProductImages";

        public BaseController()
        {
            _connectionString = ConfigurationManager.ConnectionStrings["Data"].ConnectionString;
            _classifyDataAccess = new ClassifyDataAccess(_connectionString);
            _productDataAccess = new ProductDataAccess(_connectionString);

        }

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);

            #region 站点地图

            if (ViewData["SiteMapPath"] != null)
            {
                ((List<string>)ViewData["SiteMapPath"]).Clear();
            }
            else
            {
                ViewData["SiteMapPath"] = new List<string>();
            }
            #endregion

        }
        /// <summary>
        /// 创建分页匿名对象
        /// </summary>
        /// <param name="HrefFormat">Url 格式字符串，
        /// 注意 UrlHelper 会将 {0} 转义，链接到字符串需要占用格式化字符串 {0} 占位符</param>
        /// <param name="pageSize">总页数</param>
        /// <param name="pageIndex">当前页码</param>
        /// <returns>分页匿名对象</returns>
        protected virtual object CreatePager(string HrefFormat, int pageCount, int pageIndex)
        {
            return new
            {
                PageIndex = pageIndex,
                PageCount = pageCount,

                FirstPageText = "First",
                LastPageText = "Last",

                PageDownText = "Next",
                PageUpText = "Front",

                HrefFormat = Server.UrlDecode(HrefFormat)
            };
        }

        /// <summary>
        /// 添加 SiteMapPath 路径
        /// </summary>
        /// <param name="url">路径</param>
        protected void SiteMapPathAdd(string linkText)
        {
            this.SiteMapPathAdd(linkText, null);
        }
        /// <summary>
        /// 添加 SiteMapPath 路径
        /// </summary>
        /// <param name="linkText">链接文字</param>
        /// <param name="url">路径</param>
        protected void SiteMapPathAdd(string linkText, string url)
        {
            List<string> urls = (List<string>)ViewData["SiteMapPath"];
            urls.Add(string.IsNullOrEmpty(url)
                ? linkText
                : string.Format("<a href=\"{0}\" target=\"_self\">{1}</a>", url, linkText));
            ViewData["SiteMapPath"] = urls;
        }
        /// <summary>
        /// 修改 SiteMapPath 路径
        /// </summary>
        /// <param name="oldLinkText">原链接文字</param>
        /// <param name="newLinkText">新链接文字</param>
        protected void SiteMapPathEdit(string oldLinkText, string newLinkText)
        {
            this.SiteMapPathEdit(oldLinkText, newLinkText, null);
        }
        /// <summary>
        /// 修改 SiteMapPath 路径
        /// </summary>
        /// <param name="oldLinkText">原链接文字</param>
        /// <param name="newLinkText">新链接文字</param>
        /// <param name="url">链接路径</param>
        protected void SiteMapPathEdit(string oldLinkText, string newLinkText, string url)
        {
            List<string> urls = (List<string>)ViewData["SiteMapPath"];
            int index = urls.FindIndex(s => { return s.Contains(oldLinkText); });
            if (index >= 0 && index < urls.Count)
            {
                urls[index] = string.IsNullOrEmpty(url)
                    ? newLinkText
                    : string.Format("<a href=\"{0}\" target=\"_self\">{1}</a>", url, newLinkText);
            }
            ViewData["SiteMapPath"] = urls;
        }
    }
}
